package com.ruoyi.charge.rabbitmq;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.CustomExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;

@Configuration
public class DelayExchangeConfig {
    //交换机
    public static final String DELAY_EXCHANGE = "ChargingBillDelayExchange";
    //队列
    public static final String DELAY_QUEUE = "ChargingBillDelayQueue";
    //路由
    public static final String ROUTE_KEY = "delay.#";

    //配置交换机，创建交换机放到容器中
    @Bean
    public CustomExchange getExchange() {
        HashMap<String, Object> properties = new HashMap<>();
        properties.put("x-delayed-type", "topic");
//        public CustomExchange(
//                String name, String type, boolean durable,
//        boolean autoDelete, Map<String, Object> arguments)
        //学习步骤，看源码，结合chatgpt理解
        CustomExchange customExchange =
                new CustomExchange(DELAY_EXCHANGE, "x-delayed-message",
                        false, true, properties);
        return customExchange;
    }

    //配置队列，创建队列放到容器中
    @Bean
    //org.springframework.amqp.core.Queue
    public Queue getQueue() {
        Queue queue = new Queue(DELAY_QUEUE, false);
        return queue;
    }

    //将交换机和队列绑定，通过路由将交换机和队列绑定放到容器中
    @Bean
    //org.springframework.amqp.core.Binding;
    public Binding getBinding() {
        Binding binding =
                BindingBuilder.bind(getQueue())
                        .to(getExchange()).with(ROUTE_KEY).noargs();
        return binding;
    }
    //amqp消息队列客户端框架创建rabbitTemplate对象
    //在创建rabbitTemplate对象时会从容器中取交换机，队列，绑定三个对象
}
